<?php
namespace app\common\audit\model;

use app\common\system\logic\UserLogic;
use think\Model;
use think\model\concern\SoftDelete;

class AuditFlow extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        'image' => 'json',
        'file' => 'json',
        'transfer' => 'json',
        'audit_time' => 'timestamp',
    ];

    public function audit()
    {
        return $this->hasOne(AuditResult::class, 'id', 'audit_id');
    }

    public function setImageAttr($value) {
        return empty($value) ? '[]' : json_encode($value);
    }

    public function getImageAttr($value){
        return empty($value) ? [] : json_decode($value, true);
    }

    public function setFileAttr($value) {
        return empty($value) ? '[]' : json_encode($value);
    }

    public function getFileAttr($value){
        return empty($value) ? [] : json_decode($value, true);
    }

    public function setTransferAttr($value) {
        return empty($value) ? '[]' : json_encode($value);
    }

    public function getTransferAttr($value){
        return empty($value) ? [] : json_decode($value, true);
    }

    public function getAuditorAttr($value, $data) {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['audit_user_id'], ['id', 'realname', 'status']);
    }

    public function getStatusTextAttr($value, $data) {
        $map = ['待审核', '已通过', '已拒绝'];
        $status = $data['status'];

        return $map[$status];
    }
}